package faf.web;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import faf.business.ReportManager;
import faf.business.SurveyManager;
import faf.entities.Account;
import faf.entities.Survey;

public class GraphicalReportController implements Controller {
	SurveyManager surveyManager;
	ReportManager reportManager;

	public ReportManager getReportManager() {
		return reportManager;
	}

	public void setReportManager(ReportManager reportManager) {
		this.reportManager = reportManager;
	}

	public SurveyManager getSurveyManager() {
		return surveyManager;
	}

	public void setSurveyManager(SurveyManager manager) {
		this.surveyManager = manager;
	}

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		long id = Long.parseLong(request.getParameter("id"));

		Survey survey = surveyManager.get(id);
		Account user = (Account) request.getSession().getAttribute("user");

		if(reportManager.canSeeGraphicalReport(user, survey) == true) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("survey", survey);
			
			Map<String, Integer> values = reportManager.getStateStatistics(survey.getId());
			Map<String, String> states = getGRaphaelData(values, "Etats des fiches");
			map.put("states", states);

			List<Map> questionsReports = new LinkedList<Map>();
			
			List<Map> questionsStats = reportManager.getSimpleChoiceStatistics(survey.getId());
			int i = 0;
			for(Map stats : questionsStats) {
				Map<String, String> results = getGRaphaelData((Map<String, Integer>) stats.get("statistiques"), (String) stats.get("question"));
				results.put("i", String.valueOf(i));
				questionsReports.add(results);
				i++;
			}
			
			map.put("reports", questionsReports);
			
			return new ModelAndView("graphical_report", map);
		}
		
		return new ModelAndView("report_access_denied");
	}

	private Map getGRaphaelData(Map<String, Integer> map, String title) {
		Map<String, String> data = new HashMap<String, String>();
		data.put("title", title);

		Set<String> keys = new HashSet<String>(map.keySet());

		for (String key : keys) {
			if (map.get(key).equals(0))
				map.remove(key);
		}

		StringBuffer labels_buffer = new StringBuffer();
		StringBuffer values_buffer = new StringBuffer();

		Iterator iter = map.keySet().iterator();

		while (iter.hasNext()) {
			String key = (String) iter.next();
			Integer value = (Integer) map.get(key);
			labels_buffer.append("\"" + key + " : " + value + "\"");
			values_buffer.append(value);
			if (iter.hasNext()) {
				labels_buffer.append(",");
				values_buffer.append(",");
			}
		}
		data.put("labels", labels_buffer.toString());
		data.put("values", values_buffer.toString());

		return data;
	}
}
